unit SmallStruct.Core.TSSObjectContainer;

interface

uses Classes,
  SmallStruct.Core,
  SmallStruct.Core.TSSObject;

type
  TSSObjectContainer = class(TSSObject, ISSObjectContainer)
  public
    _NameList: TStringList;
    _ObjectList: TList;
  public
    function FindObject(const AObjectName: string): Integer;
    function GetCount: Integer;
    function GetObject(const AIndex: Integer): TComponent; overload;
    function GetObject(const AObjectName: string): TComponent; overload;
    function GetObjectName(const AIndex: Integer): string;
    procedure AppendObject(const AObjectName: string; AObjectInstance: TComponent);
    procedure ReleaseObject;
    procedure RemoveObject(const AIndex: Integer); overload;
    procedure RemoveObject(const AObjectName: string); overload;
    procedure RemoveObject; overload;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

uses SmallStruct.Error,
  SysUtils;

{ TSSObjectContainer }

function TSSObjectContainer.GetCount: Integer;
begin
  Result := _NameList.Count;
end;

function TSSObjectContainer.GetObject(const AObjectName: string): TComponent;
begin
  if _NameList.IndexOf(UpperCase(Trim(AObjectName))) < 0 then
    raise ESSObjectNotFound.Create(ClassName, 'GetObject', AObjectName);
  Result := _ObjectList.Items[_NameList.IndexOf(UpperCase(Trim(AObjectName)))];
end;

function TSSObjectContainer.GetObject(const AIndex: Integer): TComponent;
begin
  if (AIndex < 0) or (AIndex >= _NameList.Count) then
    raise ESSObjectNotFound.Create(ClassName, 'GetObject', 'nil');

  Result := _ObjectList.Items[AIndex];
end;

procedure TSSObjectContainer.RemoveObject;
begin
  _NameList.Clear;
  _ObjectList.Clear;
end;

procedure TSSObjectContainer.RemoveObject(const AObjectName: string);
var
  Index: Integer;
begin
  Index := _NameList.IndexOf(AObjectName);
  if Index >= 0 then
  begin
    _NameList.Delete(Index);
    _ObjectList.Delete(Index);
  end;
end;

procedure TSSObjectContainer.RemoveObject(const AIndex: Integer);
begin
  _NameList.Delete(AIndex);
  _ObjectList.Delete(AIndex);
end;

function TSSObjectContainer.GetObjectName(const AIndex: Integer): string;
begin
  Result := _NameList.Strings[AIndex];
end;

procedure TSSObjectContainer.AppendObject(const AObjectName: string; AObjectInstance: TComponent);
begin
  if Length(Trim(AObjectName)) < 1 then
    raise ESSObjectNameIsNull.Create(ClassName, 'AppendObject');
  _NameList.Add(UpperCase(Trim(AObjectName)));
  _ObjectList.Add(AObjectInstance);
end;

function TSSObjectContainer.FindObject(const AObjectName: string): Integer;
begin
  Result := _NameList.IndexOf(AObjectName);
end;

constructor TSSObjectContainer.Create(AOwner: TComponent);
begin
  inherited;
  _NameList := TStringList.Create;
  _ObjectList := TList.Create;
end;

destructor TSSObjectContainer.Destroy;
begin
  _NameList.Free;
  _ObjectList.Free;
  inherited;
end;

procedure TSSObjectContainer.ReleaseObject;
var
  ObjectInstance: TComponent;
begin
  while _ObjectList.Count > 0 do
  begin
    ObjectInstance := _ObjectList.Items[0];
    _ObjectList.Delete(0);
    ObjectInstance.Free;
  end;
end;

end.
